package org.ssssssss.curd.controller;

import org.springframework.web.bind.annotation.*;
import org.ssssssss.curd.exception.MagicApiCurdGenerateException;
import org.ssssssss.curd.generator.MagicApiCurdGenerator;
import org.ssssssss.curd.pojo.TableColumn;
import org.ssssssss.curd.pojo.TableMeta;
import org.ssssssss.curd.strategy.RouteGetTableMetaStrategy;
import org.ssssssss.curd.util.MagicApiCurdSpringUtil;
import org.ssssssss.magicapi.core.config.MagicConfiguration;
import org.ssssssss.magicapi.core.model.JsonBean;
import org.ssssssss.magicapi.core.model.MagicEntity;
import org.ssssssss.magicapi.core.web.MagicController;
import org.ssssssss.magicapi.datasource.model.DataSourceInfo;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;

import java.util.List;

/**
 * Magic-api 生成 CURD api Controller 层
 */
public class MagicGenCrudController extends MagicController {

    /**
     * magic-api 配置对象
     */
    protected MagicConfiguration configuration;

    /**
     * 新建一个 {@link MagicGenCrudController} 对象
     * @param configuration magic-api 配置对象
     */
    public MagicGenCrudController(MagicConfiguration configuration) {
        super(configuration);
        this.configuration=configuration;
    }

    /**
     * 列出当前数据库的所有表信息
     */
    @GetMapping("/gen_curd/tables/{dataSourceId}")
    @ResponseBody
    public JsonBean<List<TableMeta>> listTables(@PathVariable("dataSourceId") String dataSourceId){
        return new JsonBean<>(getRouteGetTableMetaStrategy(dataSourceId).listTables());
    }

    /**
     * 列出当前表所有字段信息
     */
    @GetMapping("/gen_curd/table_columns/{dataSourceId}")
    @ResponseBody
    public JsonBean<List<TableColumn>> listTableColumns(@PathVariable("dataSourceId") String dataSourceId,
                                                        @RequestParam("tableName") String tableName){
        return new JsonBean<>(getRouteGetTableMetaStrategy(dataSourceId).listTableColumns(tableName));
    }

    /**
     * 生成 curd 接口
     */
    @PostMapping("/gen_curd/{dataSourceId}")
    @ResponseBody
    public JsonBean<List<TableColumn>> generateCurdApi(@PathVariable("dataSourceId") String dataSourceId,
                                                       @RequestParam("tableName") String tableName){
        //获取 dataSourceId 对应得 MagicEntity
        MagicEntity magicEntity = MagicConfiguration.getMagicResourceService().file(dataSourceId);
        if(!(magicEntity instanceof DataSourceInfo)){
            throw new MagicApiCurdGenerateException(String.format("dataSourceId = %s not DataSourceInfo ",dataSourceId));
        }
        //从 Spring 容器中构建 Magic-api CURD API 生成器 生成 CURD api
        MagicApiCurdGenerator magicApiCurdGenerator = MagicApiCurdSpringUtil.getBean(MagicApiCurdGenerator.class, magicEntity,tableName);
        magicApiCurdGenerator.generator();
        return new JsonBean<>();
    }


    /**
     * 获取 全局路由获取表元信息策略
     * @param dataSourceId 数据源ID
     */
    private RouteGetTableMetaStrategy getRouteGetTableMetaStrategy(String dataSourceId){
        //获取 dataSourceId 对应得 MagicEntity
        MagicEntity magicEntity = MagicConfiguration.getMagicResourceService().file(dataSourceId);
        if(!(magicEntity instanceof DataSourceInfo)){
            throw new MagicApiCurdGenerateException(String.format("dataSourceId = %s not DataSourceInfo ",dataSourceId));
        }
        //从 Spring 容器中获取 RouteGetTableMetaStrategy 对象
        MagicDynamicDataSource magicDynamicDataSource = MagicApiCurdSpringUtil.getBean(MagicDynamicDataSource.class);
        DataSourceInfo dataSourceInfo = (DataSourceInfo) magicEntity;
        return MagicApiCurdSpringUtil.getBean(RouteGetTableMetaStrategy.class, dataSourceInfo,magicDynamicDataSource);
    }
}
